namespace DataDuctus.MongoDb.Serialization.Extensions
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Reflection;

	internal static class AttributeExtractor
	{
		public static T GetAttribute<T>(this ICustomAttributeProvider provider, bool inherited = true) where T : Attribute
		{
			var attrs = provider.GetCustomAttributes(typeof(T), inherited);

			if (0 == attrs.Length)
			{
				return default(T);
			}

			return (T)attrs[0];
		}

		internal static IEnumerable<T> GetAttributes<T>(this ICustomAttributeProvider provider, bool inherited = true) where T : Attribute
		{
			var attrs = provider.GetCustomAttributes(typeof(T), inherited);

			return attrs.Cast<T>();
		}

		public static bool HasAttribute<T>(this ICustomAttributeProvider provider) where T : Attribute
		{
			return null != provider.GetAttribute<T>();
		}
	}
}
